package fr.gestschool.util;

import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.naming.InitialContext;

public class Utils {
	protected static ClassLoader getCurrentClassLoader(Object defaultObject){
	ClassLoader loader = Thread.currentThread().getContextClassLoader();
	if(loader == null){
	loader = defaultObject.getClass().getClassLoader();
	}
	return loader;
	}
	public static String getMessageResourceString(
	String bundleName,
	String key,
	Object params[],
	Locale locale){
	String text = null;
	ResourceBundle bundle =
	ResourceBundle.getBundle(bundleName, locale,
	getCurrentClassLoader
	(params));
	try{
	text = bundle.getString(key);
	} catch(MissingResourceException e){
	text = "?? key " + key + " not found ??";
	}
	if(params != null){
	MessageFormat mf = new MessageFormat(text, locale);
	text = mf.format(params, new StringBuffer(), null).toString();
	}
	return text;
	}
	
	public static InitialContext getInitialContext() throws Exception{
		Hashtable props = new Hashtable();
		props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
		props.put("java.naming.provider.url", "127.0.0.1");
		return new InitialContext(props);


	}
	}